<?php
class RegisteCampClass extends E_camp_student {
	public $Message;
}
class GradeClass extends E_camp_grade {
	public $Message;
}

class CampLib {
	private $studentModel;
	private $gradeModel;
	private $imageLib;
	function __construct() {
		require_once APP_LIB_PATH . 'imageLib.php';
		require_once APP_MODEL_PATH . 'e_camp_student.php';
		require_once APP_MODEL_PATH . 'e_camp_grade.php';
		$this->gradeModel = new CampGradeModel ();
		$this->imageLib = new ImageLib ();
		$this->studentModel = new CampStudentModel ();
	}
	
	private function checkInfo() {
		
		ViewData::$form_hint->StudentName = FormLib::getText64 ( 'StudentName' );
		//ViewData::$form_hint->GradeId = FormLib::getInt ('GradeId');
		//ViewData::$form_hint->GradeType = FormLib::getTinyInt4('GradeType');
		//ViewData::$form_hint->PinYin = FormLib::getTinyInt4('PinYin');
		//ViewData::$form_hint->OldSchool = FormLib::getText64 ('OldSchool');
		ViewData::$form_hint->Sex = FormLib::getTinyInt4 ( 'Sex' );
		//ViewData::$form_hint->Photo = FormLib::getText255('Photo','POST#');
		ViewData::$form_hint->BirthdayYear = FormLib::getInt ( 'BirthdayYear' );
		ViewData::$form_hint->BirthdayMonth = FormLib::getInt ( 'BirthdayMonth' );
		ViewData::$form_hint->Height = FormLib::getInt ( 'Height' );
		ViewData::$form_hint->IsOldStudent = FormLib::getTinyInt4 ( 'IsOldStudent' );
		ViewData::$form_hint->School = FormLib::getText64 ( 'School' );
		ViewData::$form_hint->Weight = FormLib::getInt ( 'Weight' );
		ViewData::$form_hint->FirstParentName = FormLib::getText64 ( 'FirstParentName' );
		ViewData::$form_hint->FirstMobile = FormLib::getMobile ( 'FirstMobile' );
		ViewData::$form_hint->FirstParentProfession = FormLib::getParentProfession ( 'FirstParentProfession' );
		ViewData::$form_hint->SecondMobile = FormLib::getMobile ( 'SecondMobile' ,'POST#');
		ViewData::$form_hint->SecondParentName = FormLib::getText64 ( 'SecondParentName','POST#' );
		ViewData::$form_hint->SecondParentProfession = FormLib::getParentProfession ( 'SecondParentProfession' ,'POST#');
		ViewData::$form_hint->HomeTel = FormLib::getText64 ( 'HomeTel','POST#' );
		ViewData::$form_hint->Email = FormLib::getEmail ( 'Email' );
		ViewData::$form_hint->QQ = FormLib::getQQ ( 'QQ' );
		ViewData::$form_hint->Address = FormLib::getText255 ( 'Address' );
		ViewData::$form_hint->Hobby = FormLib::getText255 ( 'Hobby','POST#' );
		ViewData::$form_hint->Reading = FormLib::getText64 ( 'Reading','POST#' );
		ViewData::$form_hint->Words = FormLib::getTinyInt4 ( 'Words' );
		ViewData::$form_hint->WordsNumber = FormLib::getInt ( 'WordsNumber', 'POST#' );
		ViewData::$form_hint->FromWay = FormLib::getArray ( 'FromWay' );
		ViewData::$form_hint->RefersReading = FormLib::getTinyInt4 ( 'RefersReading' );
		ViewData::$form_hint->Recite = FormLib::getTinyInt4 ( 'Recite' );
		ViewData::$form_hint->ReciteThousand = FormLib::getTinyInt4 ( 'ReciteThousand' );
		ViewData::$form_hint->ReadingInterest = FormLib::getTinyInt4 ( 'ReadingInterest' );
		ViewData::$form_hint->ReadingAbility = FormLib::getTinyInt4 ( 'ReadingAbility' );
		ViewData::$form_hint->Focus = FormLib::getTinyInt4 ( 'Focus' );
		ViewData::$form_hint->Language = FormLib::getTinyInt4 ( 'Language' );
		ViewData::$form_hint->CharacterStudent = FormLib::getArray ( 'CharacterStudent' );
		ViewData::$form_hint->Etiquette = FormLib::getTinyInt4 ( 'Etiquette' );
		ViewData::$form_hint->HabitMove = FormLib::getTinyInt4 ( 'HabitMove' );
		ViewData::$form_hint->HabitSitting = FormLib::getTinyInt4 ( 'HabitSitting' );
		ViewData::$form_hint->HabitSay = FormLib::getTinyInt4 ( 'HabitSay' );
		ViewData::$form_hint->Life = FormLib::getArray ( 'Life' );
		ViewData::$form_hint->Healthy = FormLib::getText255 ( 'Healthy','POST#' );
		ViewData::$form_hint->ActivityRestriction = FormLib::getText255 ( 'ActivityRestriction' ,'POST#');
		ViewData::$form_hint->Requirements = FormLib::getText255 ( 'Requirements' ,'POST#');
		ViewData::$form_hint->Participation = FormLib::getText255 ( 'Participation' ,'POST#');
		ViewData::$form_hint->Hope = FormLib::getArray ( 'Hope', 'POST#' );
		ViewData::$form_hint->HopeOther = FormLib::getText255 ( 'HopeOther', 'POST#' );
		ViewData::$form_hint->Goods = FormLib::getText255 ( 'Goods', 'POST#' );
		ViewData::$form_hint->Others = FormLib::getText255 ( 'Others', 'POST#' );
		ViewData::$form_hint->PinYin = FormLib::getPinYin ();
	}
	
	public function checkForm() {
		
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		$this->checkInfo ();
		return;
	}
	
	public function checkEditForm() {
		$info = '';
		$info = BaseAction::parseForm ( "POST", $info );
		ViewData::$form_data = $info;
		$this->checkInfo ();
		return;
	}
	public function registeLogic() {
		$maxId = $this->studentModel->getMaxId ();
		$info = ViewData::$form_data;
		if ($this->isExist ( $info->StudentName, $info->FirstMobile )) {
			throw new Exception ( "您已经在本站注册过，可以直接登录系统" );
		}
		$registerSession = UserSession::getRegisteInfo ();
		$info->Id = $maxId ['maxId'] + 1;
		$info->FromWay = json_encode ( $info->FromWay );
		$info->CharacterStudent = json_encode ( $info->CharacterStudent );
		$info->Life = json_encode ( $info->Life );
		$info->Hope = json_encode ( $info->Hope );
		$info->GradeId = $registerSession->GradeId;
		$info->GradeType = $registerSession->GradeType;
		$info->RegistStatus = '0';
		$photo = $_FILES ['Photo'];
		$info->Photo = '';
		if ($photo ['name']) {
			ViewData::$form_hint->Photo = $this->imageLib->filtPhoto ( $photo );
			if ($this->imageLib->filtPhoto ( $photo ) == TRUE) {
				$info->Photo = $this->imageLib->addPhoto ( $photo, 'camp', $maxId ['maxId'] + 1 );
			}
		}
		$studentId = $this->studentModel->addInfo ( $info );
		if (! $studentId) {
			throw new Exception ( "注册失败，请检查您所填写的信息是否正确" );
		}
		return $studentId;
	}
	public function isExist($StudentName, $mobile) {
		return $this->studentModel->getByLoginInfo ( $StudentName, $mobile );
	}
	
	public function editLogic($studentId) {
		$info = ViewData::$form_data;
		if ($data = $this->isExist ( $info->StudentName, $info->FirstMobile )) {
			if ($data->Id != $studentId) {
				throw new Exception ( "该姓名已经在本站注册过，请更换其他姓名，或则直接登录本站。" );
			}
		}
		if ($data = $this->isExist ( $info->StudentName, $info->SecondMobile )) {
			if ($data->Id != $studentId) {
				throw new Exception ( "该姓名已经在本站注册过，请更换其他姓名，或则直接登录本站。" );
			}
		}
		
		$info->FromWay = json_encode ( $info->FromWay );
		$info->CharacterStudent = json_encode ( $info->CharacterStudent );
		$info->Life = json_encode ( $info->Life );
		$info->Hope = json_encode ( $info->Hope );
		if (! isset ( $info->PinYin )) {
			$info->PinYin = 0;
		}
		$info->Photo = $_FILES ['Photo'];
		
		if ($info->Photo ['name']) {
			ViewData::$form_hint->Photo = $this->imageLib->filtPhoto ( $info->Photo );
			if ($this->imageLib->filtPhoto ( $info->Photo ) == TRUE) {
				$info->Photo = $this->imageLib->addPhoto ( $info->Photo, 'camp', $studentId );
			} else {
				$info->Photo == '';
			}
		} else {
			$info->Photo == '';
		}
		return $this->studentModel->setInfoById ( $info, $studentId );
	}
	public function getById($studentId) {
		$info = $this->studentModel->getById ( $studentId );
		if (! $info) {
			return FALSE;
		}
		$info->FromWay = json_decode ( $info->FromWay );
		$info->CharacterStudent = json_decode ( $info->CharacterStudent );
		$info->Life = json_decode ( $info->Life );
		$info->Hope = json_decode ( $info->Hope );
		return $info;
	}
	public function setInfoById($info, $studentId) {
		return $this->studentModel->setInfoById ( $info, $studentId );
	}
	public function addRegistedNumber($gradeId, $gradeType) {
		$info = $this->gradeModel->getByGradeId ( $gradeId );
		if (! $info) {
			return FALSE;
		}
		if ($gradeType == 1) {
			if ($info->FirstNumber <= $info->FirstRegistedNumber) {
				return FALSE;
			}
			$info->FirstRegistedNumber += 1;
		}
		if ($gradeType == 2) {
			if ($info->SecondNumber <= $info->SecondRegisterdNumber) {
				return FALSE;
			
			}
			$info->SecondRegisterdNumber += 1;
		}
		if ($gradeType == 3) {
			if ($info->FirstNumber <= $info->FirstRegistedNumber || $info->SecondNumber <= $info->SecondRegisterdNumber) {
				
				return FALSE;
			}
			$info->FirstRegistedNumber += 1;
			$info->SecondRegisterdNumber += 1;
		}
		return $this->gradeModel->setInfoById ( $info, $gradeId );
	}
	
	//==================================管理后台=======================================================
	

	public function checkPaymentForm() {
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		ViewData::$form_hint->Cost = FormLib::getCost ();
		ViewData::$form_hint->RegistStatus = FormLib::getRegistStatus ();
		return;
	}
	public function editPaymentLogic($studentId) {
		$studentInfo = $this->getById ( $studentId );
		$info = ViewData::$form_data;
		if ($info->RegistStatus == 2) {
			$model = new E_finance_daily ();
			$model->InMoney = $info->Cost;
			$model->OutMoney = 0;
			$gradeInfo = $this->gradeModel->getByGradeId ( $_GET ['gradeId'] );
			if (! $gradeInfo) {
				throw new Exception ( "请不要非法操作" );
			}
			if ($gradeInfo->ParentId == 1) {
				if ($studentInfo->GradeType == 1) {
					$gradeType = "第一期";
				}
				if ($studentInfo->GradeType == 2) {
					$gradeType = "第二期";
				}
				if ($studentInfo->GradeType == 3) {
					$gradeType = "两期连报";
				}
				$model->Description = ViewData::$form_data->StudentName . '夏令营' . $gradeType . '报名费' . ViewData::$form_data->Cost . '元';
			} else {
				$model->Description = ViewData::$form_data->StudentName . '冬令营报名费' . ViewData::$form_data->Cost . '元';
			}
			$model->Balance = ViewData::$form_data->Cost;
			$model->MoneyType = $_GET ['gradeId'];
			$model->StudentId = $studentId;
			$model->StartTime = '0000-00-00 00:00:00';
			$model->EndTime = '0000-00-00 00:00:00';
			
			require_once APP_LIB_PATH . 'financeLib.php';
			$financeLib = new FinanceLib ();
			$financeLib->addDaily ( $model );
		}
		$data->Cost = $info->Cost;
		$data->RegistStatus = $info->RegistStatus;
		return $this->studentModel->setInfoById ( $data, $studentId );
	}
	
	public function getGradeList($parentId) {
		$info=$this->gradeModel->getList ( $parentId );
		$model=new E_camp_grade();
		foreach ($info as $key=>$value){
			$model->FirstRegistedNumber=$this->getStudentCount(" GradeId=".$value['GradeId']." and RegistStatus!=0 and (GradeType=1 or GradeType=3)");
			$model->SecondRegisterdNumber=$this->getStudentCount(" GradeId=".$value['GradeId']." and RegistStatus!=0 and (GradeType=2 or GradeType=3)");
			$this->gradeModel->setInfoById($model, $value['GradeId']);
		}
		$info1=$this->gradeModel->getList ( $parentId );
		return $info1;
	}
	
	public function checkGradeForm() {
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		ViewData::$form_hint->GradeName = FormLib::getGradeName ();
		ViewData::$form_hint->FirstNumber = FormLib::getNumber ( 'FirstNumber' );
		ViewData::$form_hint->FirstRegistedNumber = FormLib::getRegistedNumber ( 'FirstRegistedNumber' );
		ViewData::$form_hint->SecondNumber = FormLib::getNumber ( 'SecondNumber' );
		ViewData::$form_hint->SecondRegisterdNumber = FormLib::getRegistedNumber ( 'SecondRegisterdNumber' );
		ViewData::$form_hint->IsShow = FormLib::getIsShow ();
		ViewData::$form_hint->Sort = FormLib::getSort ();
		ViewData::$form_hint->Description = FormLib::getDescription ();
		return;
	}
	public function addGradeLogic() {
		if (! ViewData::$form_data->ParentId) {
			throw new Exception ( "请选择类型" );
		}
		$info->ParentId = ViewData::$form_data->ParentId;
		$info->GradeName = ViewData::$form_data->GradeName;
		$info->FirstNumber = ViewData::$form_data->FirstNumber;
		$info->FirstRegistedNumber = ViewData::$form_data->FirstRegistedNumber;
		$info->SecondNumber = ViewData::$form_data->SecondNumber;
		$info->SecondRegisterdNumber = ViewData::$form_data->SecondRegisterdNumber;
		$info->IsShow = ViewData::$form_data->IsShow;
		$info->Description = ViewData::$form_data->Description;
		if (! ViewData::$form_data->Sort) {
			$info->Sort = 50;
		} else {
			$info->Sort = ViewData::$form_data->Sort;
		}
		return $this->gradeModel->addInfo ( $info );
	}
	public function setGradeLogic() {
		return $this->gradeModel->setInfoById ( ViewData::$form_data, ViewData::$form_data->GradeId );
	}
	
	public function getGradeByGradeId($gradeId) {
		return $this->gradeModel->getByGradeId ( $gradeId );
	}
	
	public function getStudentList($where) {
		return $this->studentModel->getList ( $where );
	
	}
	public function getStudentCount($where) {
		return $this->studentModel->getCount( $where );
	
	}
	public function delStudentByStudentId($studentId) {
		$info = $this->getById ( $studentId );
		if (! $info) {
			throw new Exception ( "学生已经被删除" );
		}
		if ($info->RegistStatus <1) {
			return $this->studentModel->delByStudentId ( $studentId );
		}
		$gradeInfo = $this->getGradeByGradeId ( $info->GradeId );
		if ($info->GradeType == 1) {
			$gradeInfo->FirstRegistedNumber = $gradeInfo->FirstRegistedNumber - 1;
		}
		if ($info->GradeType == 2) {
			$gradeInfo->SecondRegisterdNumber = $gradeInfo->SecondRegisterdNumber - 1;
		}
		if ($info->GradeType == 3) {
			$gradeInfo->FirstRegistedNumber = $gradeInfo->FirstRegistedNumber - 1;
			$gradeInfo->SecondRegisterdNumber = $gradeInfo->SecondRegisterdNumber - 1;
		}
		if ($gradeInfo->FirstRegistedNumber < 0) {
			$gradeInfo->FirstRegistedNumber = 0;
		}
		if ($gradeInfo->SecondRegisterdNumber < 0) {
			$gradeInfo->SecondRegisterdNumber = 0;
		}
		
		$this->gradeModel->setInfoById ( $gradeInfo, $gradeInfo->GradeId );
		return $this->studentModel->delByStudentId ( $studentId );
	}
	public function delGradeByGradeId($gradeId) {
		
		return $this->gradeModel->delByGradeId ( $gradeId );
	}
	public function exportExcel($idList) {
		$info = array ();
		$info [0] = array ('孩子姓名', '性别', '填报期数', '出生年月', '身高(cm)', '是否旧生', '就读园校年级', '体重(kg)', '监护人姓名一', '手机', '职业', '监护人姓名二', '手机', '职业', '家庭电话', 'E-mail', 'QQ', '家庭地址', '兴趣爱好专长', '曾读诵经典' );
		foreach ( $idList as $value ) {
			
			$studentInfo = $this->studentModel->getById ( $value );
			$info [$value] ['StudentName'] = $studentInfo->StudentName;
			if ($studentInfo->Sex == 1) {
				$info [$value] ['Sex'] = "男";
			} elseif ($studentInfo->Sex == 2) {
				$info [$value] ['Sex'] = "女";
			}
			
			if ($studentInfo->GradeType == 1) {
				$info [$value] ['GradeType'] = "第一期";
			} elseif ($studentInfo->GradeType == 2) {
				$info [$value] ['GradeType'] = "第二期";
			} elseif ($studentInfo->GradeType == 3) {
				$info [$value] ['GradeType'] = "两期连报";
			}
			$info [$value] ['BirthdayYear'] = $studentInfo->BirthdayYear . '年' . $studentInfo->BirthdayMonth . '月';
			$info [$value] ['Height'] = $studentInfo->Height;
			
			if ($studentInfo->IsOldStudent == 1) {
				$info [$value] ['IsOldStudent'] = "否";
			} elseif ($studentInfo->IsOldStudent == 2) {
				$info [$value] ['IsOldStudent'] = "是";
			}
			
			$info [$value] ['School'] = $studentInfo->School;
			$info [$value] ['Weight'] = $studentInfo->Weight;
			$info [$value] ['FirstParentName'] = $studentInfo->FirstParentName;
			$info [$value] ['FirstMobile'] = $studentInfo->FirstMobile;
			$info [$value] ['FirstParentProfession'] = $studentInfo->FirstParentProfession;
			$info [$value] ['SecondParentName'] = $studentInfo->SecondParentName;
			$info [$value] ['SecondMobile'] = $studentInfo->SecondMobile;
			$info [$value] ['SecondParentProfession'] = $studentInfo->SecondParentProfession;
			$info [$value] ['HomeTel'] = $studentInfo->HomeTel;
			$info [$value] ['Email'] = $studentInfo->Email;
			$info [$value] ['QQ'] = $studentInfo->QQ;
			$info [$value] ['Address'] = $studentInfo->Address;
			$info [$value] ['Hobby'] = $studentInfo->Hobby;
			$info [$value] ['Reading'] = $studentInfo->Reading;
		
		}
		require SOURCE_EXT_PATH . 'library/ExportExcelLib.php';
		$xls = new ExportExcelLib ();
		$newInfo=$xls->addArray ( $info );
		$xls->generateXML ( '夏冬令营学生列表',$newInfo );
		exit ();
	}
	
	public function delCheckStudent($idList) {
		$info = array ();
		foreach ( $idList as $value ) {
			$this->delStudentByStudentId ( $value );
		}
	}
}